#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# contact_list_action_bar.py 
#
#  Copyright (c) 2006 INdT (Instituto Nokia de Technologia)
#
#  Author: Marcelo Lira dos Santos <setanta@gmail.com>
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import pygtk
pygtk.require("2.0")
import gtk
import gobject
import tapioca
import icon_theme

from accept_contact_panel import AcceptContactPanel

class ContactListActionBar(gtk.VBox):

    __gsignals__ = {
        'add-contact' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, \
                (gobject.TYPE_STRING, )),
        'authorization-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, \
                (gobject.TYPE_STRING, gobject.TYPE_BOOLEAN)),
        'chat-session-requested' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []),
        'voip-session-requested' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []),
        'webcam-session-requested' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []),
    }


    def __init__(self, local_contact_store):
        gtk.VBox.__init__(self, spacing=4)

        self.accept_contact_panel = AcceptContactPanel(local_contact_store)
        self.accept_contact_panel.hide()
        self.pack_start(self.accept_contact_panel, expand=False, fill=True)

        self.contact_hbox = gtk.HBox(spacing=4)
        self.contact_label = gtk.Label('Contact ID:')

        self.contact_entry = gtk.Entry()
        self.contact_entry.connect('activate', self.on_contact_entry_activate)
        self.contact_entry.connect('key-press-event', self.on_contact_entry_key_press)
        
        self.contact_hbox.pack_start(self.contact_label, expand=False, fill=True)
        self.contact_hbox.pack_start(self.contact_entry, expand=True, fill=True)

        self.contact_hbox.set_no_show_all(True)

        self.pack_start(self.contact_hbox, expand=False, fill=True)

        self.actions_hbox = gtk.HBox(spacing=4)

        self.add_contact_button = gtk.ToggleButton() #stock=gtk.STOCK_ADD)
        button_hbox = gtk.HBox(spacing=2)
        icon = gtk.image_new_from_stock(gtk.STOCK_ADD, gtk.ICON_SIZE_BUTTON)
        label = gtk.Label('Invite')
        label.set_padding(4, 0)
        button_hbox.pack_start(icon, expand=False, fill=True)
        button_hbox.pack_start(label, expand=True, fill=True)
        self.add_contact_button.add(button_hbox)
        self.add_contact_button.connect('toggled', \
                self.on_add_contact_button_toggled)

        self.chat_button = gtk.Button()
        icon = gtk.Image()
        icon.set_from_pixbuf(icon_theme.icon_theme_get_default().\
                load_icon('start-chat-session', 24, 0),)
        self.chat_button.set_image(icon)
        self.chat_button.connect('clicked', self.on_chat_button_clicked)

        self.talk_button = gtk.Button()
        icon = gtk.Image()
        icon.set_from_pixbuf(icon_theme.icon_theme_get_default().\
                load_icon('start-voip-session', 24, 0),)
        self.talk_button.set_image(icon)
        self.talk_button.connect('clicked', self.on_talk_button_clicked)

        self.conference_button = gtk.Button()
        icon = gtk.Image()
        icon.set_from_pixbuf(icon_theme.icon_theme_get_default().\
                load_icon('start-webcam-session', 24, 0),)
        self.conference_button.set_image(icon)
        self.conference_button.connect('clicked', \
                self.on_conference_button_clicked)

        self.actions_hbox.pack_start(self.add_contact_button, expand=False, fill=True)
        self.actions_hbox.pack_end(self.conference_button, expand=False, fill=True)
        self.actions_hbox.pack_end(self.talk_button, expand=False, fill=True)
        self.actions_hbox.pack_end(self.chat_button, expand=False, fill=True)

        self.pack_start(self.actions_hbox, expand=False, fill=True)


    def set_capabilities(self, chat=True, talk=False, conference=False):
        self.chat_button.set_sensitive(chat)
        self.talk_button.set_sensitive(talk)
        self.conference_button.set_sensitive(conference)


    def show_add_contact(self):
        if self.add_contact_button.get_active():
            self.contact_entry.grab_focus()
        else:
            self.add_contact_button.set_active(True)


    def hide_add_contact(self):
        self.add_contact_button.set_active(False)


    def on_add_contact_button_toggled(self, widget):
        if widget.get_active():
            self.contact_entry.set_text('')
            self.contact_hbox.show()
            self.contact_label.show()
            self.contact_entry.show()
            self.contact_entry.grab_focus()
        else:
            self.contact_hbox.hide()


    def init_authorize_subscription(self, pending):
        self.accept_contact_panel.set_pending_contacts(pending)


    def add_authorize_subscription(self, contact_id):
        self.accept_contact_panel.add_pending_contact(contact_id)


    def on_chat_button_clicked(self, widget):
        self.emit('chat-session-requested')


    def on_talk_button_clicked(self, widget):
        self.emit('voip-session-requested')


    def on_conference_button_clicked(self, widget):
        self.emit('webcam-session-requested')


    def on_contact_entry_activate(self, widget):
        id = self.contact_entry.get_text()
        self.emit('add-contact', id)
        self.add_contact_button.set_active(False)


    def on_contact_entry_key_press(self, widget, event):
        if event.keyval == 65307: # ESC key
            self.add_contact_button.set_active(False)

